<?php
/**
 * Created by PhpStorm.
 * User: chenx
 * Date: 18-3-8
 * Time: 下午4:11
 */

namespace backend\controllers;


use common\models\Active;
use common\models\Goods;
use common\models\Order;
use common\models\Shop;
use common\service\CommonService;
use common\models\User;
use common\service\ConstantService;
use common\service\GlobalUrlService;
use common\service\GoodsService;

class OrderController extends BaseController
{
    /**
     * 订单列表页
     */
    public function actionIndex(){
        if($this->isGet() && $this->isAjax()){
            $query = Order::find();
            $addressee_name = $this->get('addressee_name','');
            $search_type = $this->get('search_type','');

            if(!empty($addressee_name) && !empty($search_type)){
                if($this->get('search_type') == 1){
                    $query->andWhere(['like','addressee_phone',$addressee_name]);
                } else {
                    $query->andWhere(['like','addressee_name',$addressee_name]);
                }
            }

            if(!empty($this->get('order_status'))){
                $query->andWhere(['order_status'=>$this->get('order_status')-1]);
            }
            if(!empty($this->get('start_time'))){
                $query->andWhere(['>=','created_time',strtotime($this->get('start_time'))]);
            }
            if(!empty($this->get('end_time'))){
                $query->andWhere(['<=','created_time',strtotime($this->get('end_time'))]);
            }
            $p = intval( $this->get('page',1) );
            $this->page_size = intval( $this->get('limit',10) );
            $p = ( $p > 0 )? $p : 1;
            $total_count = $query->count();

            $offset = ($p - 1) * $this->page_size;
            $orders = $query->asArray()
                ->offset($offset)
                ->limit($this->page_size)
                ->orderBy(['created_time' => SORT_DESC,'id'=>SORT_DESC])
                ->all();

//            $new_goods_attr = [];
//            if(!empty($orders)){
//                $goods_attr_ids = array_column($orders,'goods_attr_id');
//
//                //获取sku
//                $new_goods_attr = GoodsService::getCurrentArrt($goods_attr_ids);
//            }
//
//            foreach ($orders as &$item ){
//                $item['sku'] = '';
//                if(isset($new_goods_attr[$item['goods_attr_id']]) && !empty($new_goods_attr[$item['goods_attr_id']])){
//                    $item['sku'] = $new_goods_attr[$item['goods_attr_id']];
//                }
//            }
            return $this->renderPageJson($orders,$total_count,ConstantService::$response_tab_success);
        }
        return $this->render('index');
    }


    /**
     * 查看
     */
    public function actionGetInfo(){
        $id = $this->get('id');
        $data = Order::find()->where(['id'=>$id])->asArray()->one();
        $vdata  = GoodsService::getCurrentArrt([$data['goods_attr_id']]);
        $data['sku'] = !empty($vdata[$data['goods_attr_id']]) ? $vdata[$data['goods_attr_id']] :'';

        $shop = Shop::findOne(['id'=>$data['shop_id']]);
        $data['shop_name'] = $shop['shop_name'];

        $activity = Active::findOne(['id'=>$data['active_id']]);
        $data['activity_name'] = $activity['active_name'];

        $goods = Goods::findOne(['id'=>$data['goods_id']]);
//        $data['type'] = ($goods['type'] == 1)?'实体商品':'虚拟商品';
        //这里缺参团的人头像
        $users = [];

        if($data['group_id']){
            $orders = Order::find()
                ->select(['user_id'])
                ->where(['group_id' => $data['group_id']])
                ->asArray()
                ->all();

            $user_id = array_column($orders, 'user_id');
            $users = User::find()
                ->select(['avatarurl'])
                ->where(['id' => $user_id])
                ->asArray()
                ->all();
            foreach ($users as &$user) {
                $user['avatarurl'] = GlobalUrlService::buildCdnUrl($user['avatarurl']);
            }
        } else {
            //单独购买
            $user = User::find()
                ->select(['avatarurl'])
                ->where(['id'=>$data['user_id']])
                ->asArray()
                ->one();
            $user['avatarurl'] = GlobalUrlService::buildCdnUrl($user['avatarurl']);
            $users[0] = $user;
        }

        return $this->render('get-info',['data'=>$data,'users'=>$users]);
    }




}